var  BASE = BASE||{}; //全局公用方法
BASE.POPUP = (function () {
    var SELECTEDCOVERS = []; /*选中的封面*/
    
    /*分类处理选择start*************************************/
    /*根据兴趣方向主分类获取下级*/
    function selectInterestPrarent(id,callback) {
        $('#parent_'+id).addClass('hover').siblings().removeClass('hover');
        $('#interestData').val(id);
        if(!id){
            $('#interestChildData').val('');
            return;
        }
        _getInterest(id,function (data) {
            $('#interestChildData').html(template('interestChildData_tpl',{data:data}));
            $('#interestChildData_btn').html(template('interestChildData_btn_tpl',{data:data}));
            if(callback &&typeof callback=='function'){
                callback(data);
                return;
            }
            $('#interestChildData').val('');
           /* if(data.length>0 && data[0].id){
                var childId = data[0].id;
                $('#interestChildData').val(childId);
                $('#child_'+childId).addClass('hover').siblings().removeClass('hover');
            }*/
        })
    }
    /*选择子分类*/
    function selectInterestChild(id) {
        $('#child_'+id).addClass('hover').siblings().removeClass('hover');
        $('#interestChildData').val(id);
    }
    /*获取兴趣方向*/
    function _getInterest(parentId,callback,flag) {
        var sendData = {
            parentId:parentId || '',
        };
        BASE.apiGet('/device/mainCategory/list', sendData,function (data) {
            BASE.setStore('mainCategory',data);
            if(callback &&typeof callback=='function' && !flag){
                callback(data);
                return;
            }
            var result = _initInterestData(data);
            $('#interestData').html(template('interestData_tpl',{data:result}));
            $('#interestData_btn').html(template('interestData_btn_tpl',{data:result}));
            var id = result[0].id;
            $('#interestData').val(id);
            $('#parent_'+id).addClass('hover').siblings().removeClass('hover');
            selectInterestPrarent(id);
            if(callback &&typeof callback=='function' && flag){
                callback(data);
            }
        });
    }

    /*获取的兴趣方向的数据处理*/
    function _initInterestData(arr) {
        var result = [];
        arr.forEach(function (item) {
            if (item.parentId === '0') {
                result.push(item);
            }
        });
        return result;
    }
    function getParentIdByid(id,callback) {
        var result = '';
        _getInterest('',function (data) {
            data.forEach(function (v) {
                if(v.id == id){
                    if(v.parentId != 0){
                        result = v.parentId;
                    }
                }
            });
            var catagory = _initInterestData(data);
            $('#interestData').html(template('interestData_tpl',{data:catagory}));
            $('#interestData_btn').html(template('interestData_btn_tpl',{data:catagory}));
            callback(result)
        })

    }
    /*分类处理选择end*************************************/

    /*素材star*************************************/
    /*获取素材分类*/
    function coversCategory() {
        BASE.apiGet('/device/materialCategory/list',function (res) {
            $('#coversCategory').html(template('coversCategory_tpl',{data:res}));
            if(res.length>0 && res[0].id){
                $('#coversCategory').val(res[0].id);
                getCoversByCategory(res[0].id)
            }
        })
    }
    /*获取分类下的素材*/
    function getCoversByCategory(id) {
        var sendData = {
            pageNum:1,
            pageSize:10,
            randomIs:true,
            categoryId:id,
        };
        BASE.apiGet('/device/material/list',sendData,function (res) {
            $('#answerCovers').html(template('answerCovers_tpl',{list:res.list}));
        })
    }
    /*封面选择*/
    function selectCovers(obj) {
        var id = $(obj).data('id');
        var url = $(obj).data('url');
        initShowCovers(url,id);
    }
    /*是否已经选择了该封面*/
    function _hasThisData(url) {
        var flag = false;
        BASE.POPUP.SELECTEDCOVERS.forEach(function (v) {
            if(v.url == url){
                flag = true;
                return false;
            }
        });
        return flag;
    }
    /*上传封面*/
    function uploadCoverPrev(obj) {
        if(BASE.POPUP.SELECTEDCOVERS.length>=4){
            BASE.clearpop('最多可选4张封面');
            return;
        }
        $(obj).next().trigger('click');
    }
    /*上传封面*/
    function uploadCover(obj) {
        var length = obj.files.length;
        var selLen = BASE.POPUP.SELECTEDCOVERS.length || 0;
        var resLen =  4- selLen;
        if(length > resLen){
            BASE.clearpop('最多可选'+resLen+'张封面');
            return;
        }
        for (var i = 0; i < obj.files.length; i++) {
            var obj1 = obj.files[i];
            BASE.UPLOADANSWER.uploadImage(obj1,function (url) {
                initShowCovers(url,'');
            })
        }
    }
    /*封面移除*/
    function removeCover(obj) {
        var index = $(obj).data('index');
        $(obj).parent().remove();
        BASE.POPUP.SELECTEDCOVERS.splice(index,1);
    }
    /*封面显示*/
    function initShowCovers(url,id) {
        if(BASE.POPUP.SELECTEDCOVERS.length>=4){
            BASE.clearpop('最多可选4张封面');
            return;
        }
        if(_hasThisData(url)){
            return;
        }
        if(url){
            BASE.POPUP.SELECTEDCOVERS.push({
                url:url,
                id:id,
            });
        }
        $('#selectedCovers').html(template('selectedCovers_tpl',{list:BASE.POPUP.SELECTEDCOVERS}));
    }
    /*获取封面URL*/
    function _getCovers() {
        var results = [];
        BASE.POPUP.SELECTEDCOVERS.forEach(function (v) {
            results.push(v.url);
        });
        return results;
    }
    /*素材end*************************************/
    
    return {
        /*分类*/
        selectInterestPrarent:selectInterestPrarent,
        getInterest:_getInterest,
        selectInterestChild:selectInterestChild,
        getParentIdByid:getParentIdByid,
        /*封面素材*/
        initShowCovers:initShowCovers,
        coversCategory:coversCategory,
        getCoversByCategory:getCoversByCategory,
        selectCovers:selectCovers,
        removeCover:removeCover,
        uploadCover:uploadCover,
        uploadCoverPrev:uploadCoverPrev,
        getCovers:_getCovers,
        SELECTEDCOVERS:SELECTEDCOVERS,
    }
})();